export default function useTableData(getTableDataFn, filterOption = {}) {
  // 数据
  const tableData = ref([])
  // 分页
  const page = reactive({
    total: 0, // 总页数
    pageSize: 50, // 分页大小
    currentPage: 1, // 当前页
    start: 0, // 从第几个开始
  })
  // 加载态
  const loading = ref(false)

  const getTableData = async () => {
    loading.value = true
    try {
      const { total, result } = await getTableDataFn({
        start: page.start,
        length: page.pageSize,
        ...filterOption,
      })
      page.total = total
      tableData.value = result
    } catch (error) {
      console.log(error)
    } finally {
      loading.value = false
    }
  }

  onMounted(() => {
    getTableData()
  })

  watch([() => page.start, () => page.pageSize], (newValue, oldValue) => {
    getTableData()
  })

  return {
    loading,
    page,
    tableData,
    getTableData,
  }
}
